package com.center.xx1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Centers {

	private static ReentrantReadWriteLock fdbyLock = new ReentrantReadWriteLock(false);
	private static ReentrantReadWriteLock scxfLock = new ReentrantReadWriteLock(false);
	
	private static ConcurrentMap<String,BlockingQueue<Object>> fbdyqueueMap = new ConcurrentHashMap<String,BlockingQueue<Object>>();
	private static ConcurrentMap<String,BlockingQueue<Object>> scxfqueueMap = new ConcurrentHashMap<String,BlockingQueue<Object>>();
	private static ConcurrentMap<String,Map<String,Object>> messageMap = new ConcurrentHashMap<String,Map<String,Object>>();
	private static ConcurrentMap<String,Set<String>> dyzMap = new ConcurrentHashMap<String,Set<String>>();
	
	public void createOrGetQueue(String queueName,String type){
		if(type.equals("fbdy")){
			fdbyLock.writeLock().lock();
			if(fbdyqueueMap.containsKey(queueName)){
				return;
			}
			Map<String,Object> objMap = new HashMap<String,Object>();
			BlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(30);
			fbdyqueueMap.put(queueName, queue);
			messageMap.put(queueName+"_fbdy", objMap);
			Set<String> dyzList = new HashSet<String>();
			dyzMap.put(queueName, dyzList);
			fdbyLock.writeLock().unlock();
		}else if(type.equals("scxf")){
			scxfLock.writeLock().lock();
			if(scxfqueueMap.containsKey(queueName)){
				return;
			}
			Map<String,Object> objMap = new HashMap<String,Object>();
			BlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(30);
			scxfqueueMap.put(queueName, queue);
			messageMap.put(queueName+"_scxf", objMap);
			scxfLock.writeLock().unlock();
		}
	}
	
	public void delQueue(String queueName,String type){
		if(type.equals("fbdy")){
			fbdyqueueMap.remove(queueName);
			messageMap.remove(queueName+"_fbdy");
		}else if(type.equals("scxf")){
			fbdyqueueMap.remove(queueName);
			messageMap.remove(queueName+"_scxf");
		}
	}
	
	public void inQueue(String queueName,String type,Object o)throws Exception{
		if(type.equals("fbdy")){
			inScxfQueue(queueName, o);
		}else if(type.equals("scxf")){
			inFbdyQueue(queueName, o);
		}
	}
	
	public Object outQueue(String queueName,String type)throws Exception{
		if(type.equals("fbdy")){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("dyz", dyzMap.get(queueName));
			map.put("data", outScxfQueue(queueName));
			return map;
		}else if(type.equals("scxf")){
			return outFbdyQueue(queueName);
		}else{
			return null;
		}
	}
	
	private void inScxfQueue(String queueName,Object o) throws Exception{
		fbdyqueueMap.get(queueName).put(o);
	}
	
	private Object outScxfQueue(String queueName) throws Exception{
		return fbdyqueueMap.get(queueName).take();
	}
	
	private void inFbdyQueue(String queueName,Object o) throws Exception{
		fbdyqueueMap.get(queueName).put(o);
	}
	
	private Object outFbdyQueue(String queueName) throws Exception{
		return fbdyqueueMap.get(queueName).take();
	}
	
	public boolean dyzRegister(String queueName,String registerId){
		if(dyzMap.containsKey(queueName)){
			dyzMap.get(queueName).add(registerId);
			return true;
		}else{
			return false;
		}
	}
	
	public boolean dyzUnRegister(String queueName,String registerId){
		if(dyzMap.containsKey(queueName)){
			dyzMap.get(queueName).remove(registerId);
			return true;
		}else{
			return false;
		}
	}
	
}
